//
//  HYCategoryViewController.m
//  XPlayer
//
//  Created by liguoqing on 2023/11/8.
//

#import "HYCategoryViewController.h"
#import "HYHomeMainView.h"

@interface HYCategoryViewController ()<JXCategoryViewDelegate>

@end

@implementation HYCategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - Custom Accessors
// 分页菜单视图
- (JXCategoryBaseView *)categoryView
{
    if (!_categoryView) {
        _categoryView = [self preferredCategoryView];
        _categoryView.backgroundColor = [CommUtls dynamicColorWithLightModeHex:@"#F5F5F5" darkModeHex:@"#24262E"];
        _categoryView.delegate = self;
        // !!!: 将列表容器视图关联到 categoryView
        _categoryView.listContainer = self.listContainerView;
    }
    return _categoryView;
}

// 列表容器视图
- (JXCategoryListContainerView *)listContainerView
{
    if (!_listContainerView) {
        _listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
        _listContainerView.backgroundColor = [CommUtls dynamicColorWithLightModeHex:@"#FFFFFF" darkModeHex:@"#181A22"];
    }
    return _listContainerView;
}

#pragma mark - Actions

- (void)navigationRightBarButtonAction:(id)sender
{
    JXCategoryIndicatorView *componentView = (JXCategoryIndicatorView *)self.categoryView;
    for (JXCategoryIndicatorComponentView *view in componentView.indicators) {
        if (view.componentPosition == JXCategoryComponentPosition_Top) {
            view.componentPosition = JXCategoryComponentPosition_Bottom;
        } else {
            view.componentPosition = JXCategoryComponentPosition_Top;
        }
    }
    [componentView reloadDataWithoutListContainer];
}

#pragma mark - Public

- (JXCategoryBaseView *)preferredCategoryView
{
    return [[JXCategoryBaseView alloc] init];
}

- (CGFloat)preferredCategoryViewHeight
{
    return kIsIPad?64:44;
}

#pragma mark - JXCategoryViewDelegate

// 点击选中或者滚动选中都会调用该方法。适用于只关心选中事件，不关心具体是点击还是滚动选中的。
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index
{
    CLog(@"%@", NSStringFromSelector(_cmd));
    
    // 侧滑手势处理
    self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
}

// 滚动选中的情况才会调用该方法
- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index
{
    CLog(@"%@", NSStringFromSelector(_cmd));
}

#pragma mark - JXCategoryListContainerViewDelegate

// 返回列表的数量
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView
{
    return self.titles.count;
}

// 返回各个列表菜单下的实例，该实例需要遵守并实现 <JXCategoryListContentViewDelegate> 协议
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    HYHomeMainView *list = [[HYHomeMainView alloc] init];
    return list;
}

@end
